package com.sgb.kjwl.viewmodel.manage

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import android.view.Gravity
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.sgb.contract.model.ContractListSearchBean.ContractListInfo
import com.sgb.contract.utils.ArouterConfigs
import com.sgb.contract.view.widget.TagBuilder
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.manage.WebsiteContractEntity
import com.sgb.link.entity.PickInvitedApply
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.ListResponse
import retrofit2.Call
import java.util.*
import kotlin.collections.ArrayList


class WebsiteContractListViewModel (application: Application) : AndroidViewModel(application) {

    private var map = HashMap<String, Any>()
    private val page = 1
    private val limit = 10

    //招采--邀请报名--列表
    var pickInvitedApplyList = MutableLiveData<List<PickInvitedApply>>()

    //招采--邀请竞价--列表
    var pickInvitedBiddingList = MutableLiveData<List<PickInvitedApply>>()

    //合约列表
    var mWebsiteContractList = MutableLiveData<List<WebsiteContractEntity>>()


    private var inviteMap = HashMap<String, Any>()

    var isEnableInvite = MutableLiveData<Boolean>()

    /**
     * 跳转到合同详情（签署合同）
     */
    fun jumpToSignContractActivity(activity : Activity, bean: ContractListInfo) {
        if (null == bean.localUrl) {
            showDialog(activity, "", "当前合同暂无内容\n请在电脑端编辑后再操作！")
            return
        }
        if (1 == bean.status && bean.isTaker) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                .withBoolean("isShowOperationLayout", false)
                .withString("contractId", "" + bean.id)
                .navigation()
        } else if (2 == bean.status && 6 == bean.signTag) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                .withBoolean("isShowOperationLayout", false)
                .withString("contractId", "" + bean.id)
                .navigation()
        } else if (2 == bean.status && 7 == bean.signTag) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                .withString("contractId", "" + bean.id)
                .navigation()
        } else if (3 == bean.status || 4 == bean.status || 5 == bean.status) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                .withBoolean("isShowOperationLayout", false)
                .withString("contractId", "" + bean.id)
                .navigation()
        } else if (1 == bean.status && !TextUtils.isEmpty(bean.deadline)) {
            if (bean.isTaker) {
                ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                    .withBoolean("isShowOperationLayout", false)
                    .withString("contractId", "" + bean.id)
                    .navigation()
            } else {
                showDialog(activity,"takerEditContract", "当前合同发起方修改中")
            }
        }
    }


    /**
     * 公共弹窗
     * fixme:有时间再来优化此处 cyj
     */
    private fun showDialog(activity : Activity, tag : String, message : String) {
        // 当前合同第X页存在印章覆盖\n请重新拖拽！
        val builder = TagBuilder(activity)
            .setTag(tag)
            .title("温馨提示")
            .content(message)
            .contentGravity(Gravity.CENTER)
            .cancelable(false)
            .confirmCallback {

            }
        builder.singleBtn(true)
        builder.confirm("知道了")
        builder.show()
    }

    //邀请招标-招标信息列表
    fun findSignPage(isLoadMore : Boolean) {
        map["page"] = if (isLoadMore) page + 1 else 1
        map["limit"] = limit
        map["tenderName"] = ""
        APIManager.getInstance().pickAPI.findSignPage(map)
            .enqueue(object : ICallBack<BaseEntity<ListResponse<List<PickInvitedApply>>>>(true, true) {
                override fun onSuccess(data: BaseEntity<ListResponse<List<PickInvitedApply>>>) {
                    if (data != null && data.data != null && data.data.list != null){
                        pickInvitedApplyList.postValue(data.data.list)
                    } else {
                        pickInvitedApplyList.postValue(ArrayList())
                    }
                }

                override fun onFailure(
                    call: Call<BaseEntity<ListResponse<List<PickInvitedApply>>>>,
                    t: Throwable
                ) {
                    pickInvitedApplyList.postValue(ArrayList())
                }
            })
    }


    //邀请竞价-竞价信息-列表
    fun getFindBidPage(isLoadMore: Boolean) {
        map["page"] = if (isLoadMore) page + 1 else 1
        map["limit"] = limit
        map["keyword"] = ""
        APIManager.getInstance().pickAPI.getFindBidPage(map)
            .enqueue(object : ICallBack<BaseEntity<ListResponse<List<PickInvitedApply>>>>() {
                override fun onSuccess(data: BaseEntity<ListResponse<List<PickInvitedApply>>>) {
                    if (data != null && data.data != null && data.data.list != null){
                        pickInvitedBiddingList.postValue(data.data.list)
                    } else {
                        pickInvitedBiddingList.postValue(ArrayList())
                    }
                }

                override fun onFailure(
                    call: Call<BaseEntity<ListResponse<List<PickInvitedApply>>>>,
                    t: Throwable
                ) {
                    pickInvitedBiddingList.postValue(ArrayList())
                }
            })
    }

    //企业网站-投标、竞价-判断是否可邀请
    /**|
     * tenderNo 列表的item no
     * type //1邀请报名2邀请竞价
     * companyNo //当前身份所在企业No
     * storeNo //当前网站的no
     */
    fun websiteIsEnableInvite(tenderNo : String, type : String, companyNo : String, storeNo : String){
        inviteMap["tenderNo"] = tenderNo
        inviteMap["type"] = type
        inviteMap["companyNo"] = companyNo
        inviteMap["storeNo"] = storeNo
        APIManager.getInstance().websiteInfoAPI.websiteIsEnableInvite(inviteMap).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null && data.data is Boolean){
                    isEnableInvite.postValue(data.data as Boolean)
                } else {
                    isEnableInvite.postValue(false)
                }
            }

            override fun onFailure(call: Call<BaseEntity<*>>, t: Throwable) {
                super.onFailure(call, t)
                isEnableInvite.postValue(false)
            }
        })
    }

    /**
     * 合同列表
     *  companyNo 对方网站no
     */
    fun getContractList(companyNo: String){
        APIManager.getInstance().websiteInfoAPI.websiteContractList(companyNo).enqueue(object : ICallBack<BaseEntity<ListResponse<List<WebsiteContractEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<WebsiteContractEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    mWebsiteContractList.postValue(data.data.list)
                } else {
                    mWebsiteContractList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<WebsiteContractEntity>>>>,
                t: Throwable
            ) {
                super.onFailure(call, t)
                mWebsiteContractList.postValue(ArrayList())
            }

        })
    }

}